package cn.jsj.hzu.qm.facecloud.ciuser.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.hzu.jsj.utils.BeanFactory;
import cn.jsj.hzu.qm.facecloud.ciuser.pojo.Cilabrecord;
import cn.jsj.hzu.qm.facecloud.ciuser.pojo.Ciuser;
import cn.jsj.hzu.qm.facecloud.ciuser.service.CilabrecordService;

/**
 * Servlet implementation class CilabrecordServlet
 */
public class CilabrecordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;encoding=UTF-8");
		
		/* 允许跨域的主机地址 */
		response.setHeader("Access-Control-Allow-Origin", "*");  
		/* 允许跨域的请求方法GET, POST, HEAD 等 */
		response.setHeader("Access-Control-Allow-Methods", "*");  
		/* 重新预检验跨域的缓存时间 (s) */
		response.setHeader("Access-Control-Max-Age", "3600");  
		/* 允许跨域的请求头 */
		response.setHeader("Access-Control-Allow-Headers", "*");  
		/* 是否携带cookie */
		response.setHeader("Access-Control-Allow-Credentials", "true");
		
		PrintWriter out = response.getWriter();

		String modeString = request.getParameter("mode");
		if (modeString == null) {
			response.getWriter().write("no mode");
			return;
		}
		

		int mode;
		mode = Integer.parseInt(modeString);
		
		CilabrecordService cilabrecordService = BeanFactory.getBean("cilabrecordService");
		switch (mode) {
		//新增
		case 11:{
			//针对未登录用户的非法访问
			Ciuser ciuser = (Ciuser) request.getSession().getAttribute("ciuser");
			if (ciuser==null|| ciuser.getType() == 0) {
				response.sendError(566, "没有登录");
				return;
			}
			
			
			String labnameString = request.getParameter("labname");
			String recordString = request.getParameter("record");
			if (labnameString == null || recordString == null) {
				response.sendError(566, "参数缺失");
				return;
			}
			
			Cilabrecord cilabrecord = new Cilabrecord();
			cilabrecord.setLabname(labnameString);
			cilabrecord.setRecord(recordString);
			cilabrecord.setWho(ciuser.getName());
			
			int i = cilabrecordService.addCilabrecord(cilabrecord);
			response.getWriter().write("{'state':'"+i+"'}");
			return;
		}
		
		//查
		
		//查询表中记录的总数
		case 41:{
			out.write("["+cilabrecordService.getCilabrecordsCount()+"]");
			return;
		}
		
		//分页查询所有的签到记录
		 
		case 42:{
			String pageString = request.getParameter("page");
			String perpageString = request.getParameter("perpage");
			
			if (pageString == null||perpageString == null) {
				response.sendError(567, "参数缺失");
				return;
			}
			
			
			int page = Integer.parseInt(pageString);
			int perpage = Integer.parseInt(perpageString);
			
			List<Cilabrecord> cilabrecords = cilabrecordService.getCilabrecordsByPage(page, perpage);
			
			if(cilabrecords == null) {
				out.write("[]");
				return;
			}
			
			
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append('[');
			for(Cilabrecord mycilabCilabrecord : cilabrecords) {
				stringBuilder.append(mycilabCilabrecord.toJsonString());
				stringBuilder.append(',');
			}
			stringBuilder.append(']');
			out.write(stringBuilder.toString());
			return;
			
			
			
		}
		
		
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
